<template>
  <div class="dicPage">
    <el-tabs v-model="tabIndex" type="card" @tab-click="handleClick">
      <el-tab-pane label="全部" name="0"></el-tab-pane>
      <el-tab-pane label="车辆信息" name="1"></el-tab-pane>
      <el-tab-pane label="单位信息" name="2"></el-tab-pane>
      <el-tab-pane label="司机信息" name="3"></el-tab-pane>
      <el-tab-pane label="申请用车" name="4"></el-tab-pane>
    </el-tabs>
    <el-row>
      <el-col :sm="4">
        <div class="g-title">字典数目({{data.length}}个）</div>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :sm="12">
        <el-table
          :data="data"
          border
          style="width: 100%"
          @row-click="rowClick"
          height="calc(100vh - 300px)"
        >
          <el-table-column type="index" align="center" width="100" label="序号"></el-table-column>
          <el-table-column align="center" prop="name" label="名称">
            <template slot-scope="scope">
              <div>
                <el-link type="primary">{{scope.row.name}}</el-link>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="200" align="center" label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="primary" size="mini" @click.stop="addBaseconst(scope.row.id)">新建项</el-button>
                <el-button type="danger" size="mini" @click.stop="changeBaseTypeById(scope.row)">修改</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <el-col :sm="12">
        <el-table :data="children" border style="width: 100%" height="calc(100vh - 300px)">
          <el-table-column type="index" align="center" width="100" label="序号"></el-table-column>
          <el-table-column align="center" prop="name" label="名称">
            <template slot-scope="scope">
              <div>
                <el-link type="primary">{{scope.row.name}}</el-link>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="200" align="center" label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="danger" size="mini" @click="changeBaseconstById(scope.row)">修改</el-button>
                <el-button type="warning" size="mini" @click="removeBaseconstById(scope.row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <add ref="addBaseconstComp" @finish="findBaseconstByTypeId"></add>
    <edit ref="changeBaseTypeByIdComp" @finish="fetchData"></edit>
    <editSub ref="changeBaseconstByIdComp" @finish="findBaseconstByTypeId"></editSub>
  </div>
</template>
<script>
import "./index.scss";
import {
  listJson,
  findBaseconstByTypeId,
  removeBaseconstById
} from "@/api/dictionary";
import add from "./components/add";
import edit from "./components/edit";
import editSub from "./components/editSub";
export default {
  components: { edit, add, editSub },
  data() {
    return {
      tabIndex: "0",
      children: [],
      data: []
    };
  },
  async created() {
    let r = await this.fetchData();
    if (r && r[0]) {
      this.findBaseconstByTypeId(r[0].id);
    }
  },
  methods: {
    async handleClick() {
      let r = await this.fetchData();
      if (r && r[0]) {
        this.findBaseconstByTypeId(r[0].id);
      }
    },
    //新增
    addBaseconst(basetypeId) {
      this.$refs.addBaseconstComp.show(basetypeId);
    },
    //修改
    changeBaseTypeById(data) {
      this.$refs.changeBaseTypeByIdComp.show(data);
    },
    //修改子项
    changeBaseconstById(data) {
      this.$refs.changeBaseconstByIdComp.show(data);
    },
    //删除子项
    async removeBaseconstById(data) {
      try {
        let r = await removeBaseconstById({ baseConstId: data.id });
        if (r && r.error == 0) {
          this.findBaseconstByTypeId(data.zbasetype.id);
        } else {
          this.$message({
            message: (r && r.msg) || "操作失败",
            type: "error"
          });
        }
      } catch (error) {
        this.$message({
          message: (error && error.message) || "操作失败",
          type: "error"
        });
      }
    },
    rowClick(rowData) {
      this.findBaseconstByTypeId(rowData.id);
    },
    del(index) {
      //  this.data[this.childrenIndex].children.splice(index, 1);
    },
    //请求分类数据并返回promise 如果成功则继续请求第一个大分类的子分类
    fetchData() {
      return new Promise(async (resolve, reject) => {
        try {
          let data = await listJson({ type: this.tabIndex, pageSize: 100 });
          if (data && data.error == 0) {
            this.data.splice(0, this.data.length, ...data.data);
            resolve(data.data);
          } else {
            this.$message({
              message: (data && data.msg) || "获取数据失败",
              type: "error"
            });
            reject();
          }
        } catch (error) {
          this.$message({
            message: (error && error.message) || "获取数据失败",
            type: "error"
          });
          reject();
        }
      });
    },
    async findBaseconstByTypeId(basetypeId) {
      try {
        let data = await findBaseconstByTypeId({
          basetypeId
        });
        if (data && data.error == 0) {
          this.children.splice(0, this.children.length, ...data.data);
        } else {
          this.$message({
            message: (data && data.msg) || "获取数据失败",
            type: "error"
          });
        }
      } catch (error) {
        this.$message({
          message: (error && error.message) || "获取数据失败",
          type: "error"
        });
      }
    }
  }
};
</script>